package com.sh.xxj.rebot.service;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

import com.hn.comm.Tools;
import com.hn.net.Kit;

/**
 * 声音服务
 * 
 * @author sh
 * 
 */
public class VoiceService {

	public static byte[] getVoiceBytes(String word) throws Exception {
		// http://localhost:8888/data/yuannana.mp3
		// http://translate.google.cn/translate_tts?ie=UTF-8&q=%E7%9F%A5%E9%81%93%E4%BA%86&tl=zh-CN&total=1&idx=0&textlen=2
		//http://translate.google.cn/translate_tts?ie=UTF-8&q=Orange&tl=en&total=1&idx=0&textlen=6&client=t
		
//		http://translate.google.cn/translate_tts?ie=UTF-8&q=%E9%95%BF%E5%AE%89%E6%B1%BD%E8%BD%A6&tl=zh-CN&total=1&idx=0&textlen=4&client=t&prev=input
		
		String url = ConfigService.TRANSLATE_VOICE_API;
		word=java.net.URLEncoder.encode(word, "utf-8");
		url = url.replaceAll("@p1@", word);
		
		
		//微软发音
//		String url = "http://api.microsofttranslator.com/V2/http.svc/Speak?appId=TtZGvAr9c_BueE8UPA7uZXxvxY5Tlk8J7g2FIR4FzQgFS6DHDaM8jLpaqqa7J-YVv&text=";
//		url += java.net.URLEncoder.encode(word, "utf-8");
//		url += "&language=zh-CN&format=audio%2fmp3";
		
		// System.out.println(url);
		byte[] arr = httpRequestBytes(url, null, "GET", "utf-8");
		return arr;
	}

	/**
	 * 写文件
	 * @param data
	 * @param filePath
	 * @param filename
	 */
	public static void writeToFile(byte[] data, String filePath, String filename) {
		//创建文件夹
		Tools.newFolder(filePath);
		Tools.deleteFolder( filePath+File.separator+filename+".mp3");
		Tools.writeFile(data, filePath+File.separator+filename+".mp3");
	}

	/**
	 * 
	 * 
	 * @param URL
	 * 
	 * @param requestContent
	 *            请求内容
	 * @param requestMethod
	 *            POST/GET
	 * @param charset
	 *            编码集
	 * @return
	 */
	public static byte[] httpRequestBytes(String URL, String requestContent,
			String requestMethod, String charset) throws Exception {
		String strScript = null;
		URL url = new URL(URL);

		HttpURLConnection httpUrlConn = (HttpURLConnection) url
				.openConnection();
		httpUrlConn.setRequestProperty("User-agent", "Mozilla/4.0");
		// 默认120秒超时
		httpUrlConn.setConnectTimeout(120000);
		httpUrlConn.setReadTimeout(120000);
		httpUrlConn.setRequestMethod(requestMethod);
		httpUrlConn.setDoOutput(true);
		if (requestContent != null) {
			httpUrlConn.getOutputStream().write(requestContent.getBytes());
			httpUrlConn.getOutputStream().flush();
			httpUrlConn.getOutputStream().close();
		}
		InputStream is = httpUrlConn.getInputStream();

		byte[] arrRecv = getBytes(is, 9999);

		is.close();
		return arrRecv;
	}

	/**
	 * 读取byte[]
	 * 
	 * @param inputstream
	 *            InputStream
	 * @return byte[]
	 * @throws IOException
	 */
	public static byte[] getBytes(InputStream inputstream, int iBufferSize)
			throws IOException {
		byte[] bArr = new byte[iBufferSize];

		int iIndex = 0;
		int iLength = 0;
		int iTotalLength = 0;

		while ((iLength = inputstream.read(bArr, iIndex, bArr.length - iIndex)) != -1) {
			iIndex += iLength;
			if (iLength != -1) {
				iTotalLength += iLength;
			}
			if (iIndex >= bArr.length) {
				byte[] arrbTemp = new byte[bArr.length];
				System.arraycopy(bArr, 0, arrbTemp, 0, bArr.length);
				bArr = null;
				bArr = new byte[arrbTemp.length + iBufferSize];
				System.arraycopy(arrbTemp, 0, bArr, 0, arrbTemp.length);
				arrbTemp = null;
			}
		}
		byte[] bBack = new byte[iTotalLength];
		System.arraycopy(bArr, 0, bBack, 0, bBack.length);
		bArr = null;

		return bBack;
	}

}
